//
//  KSKAKatsudouController.m
//  DashboardHD
//
//  Created by 楊亜玲 on 17/3/27.
//  Copyright © 2017年 Totyu. All rights reserved.
//

#import "KSKAKatsudouController.h"
#import "KSKAKatsudouTool.h"
#import "KSKAKatsudouCell.h"

@interface KSKAKatsudouController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

/** 一周的数据数组(包含7个模型数组数组) */
@property (nonatomic, strong) NSArray *dataArray;

/** 一周的日期数组(用于label显示) */
@property (nonatomic, strong) NSArray *dateArray;

@end

@implementation KSKAKatsudouController

static NSString * const reuseID = @"katsudouCell";


- (void)viewDidLoad {
    [super viewDidLoad];
    if (_actionid.length) {
        [self loadDataWithBasedate:self.mondayStr actionid:self.actionid sumflg:@"d"];
    }else{
        [self loadDataWithBasedate:self.mondayStr actionid:nil sumflg:@"d"];
    }
}


/**
 请求网络数据

 @param dateStr 基准日
 @param actionid 活动id
 @param sumflg 日／周／月 数据分类
 */
-(void)loadDataWithBasedate:(NSString *)dateStr actionid:(NSString *)actionid sumflg:(NSString *)sumflg{
    
    KSKAKatsudouParam *param = [[KSKAKatsudouParam alloc]init];
    param.userid0 = @"0002";
    param.basedate = dateStr;
    param.sumflg = sumflg;
    [KSKAKatsudouTool katsudouDailyListWithParam:param success:^(NSArray *array) {
        if (array.count > 0) {
            // 抽取某个活动的数据
            if (actionid.length) {
                NSMutableArray *tmpArr = [NSMutableArray array];
                for (KSKAKatsudouResult *model in array) {
                    if ([model.actionid isEqualToString:_actionid]) {
                        [tmpArr addObject:model];
                    }
                }
                self.dataArray = tmpArr;
            }else{
                self.dataArray = array;
            }
            
            [self.collectionView reloadData];
        }else{
#warning todo
            KSKLog(@"网络返回空数组");
        }
        
    } failure:^(NSError *error) {
        
#warning todo
    }];
}


#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    // 1.创建cell
    KSKAKatsudouCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
    // 2.传递数据
    KSKAKatsudouResult *result = [[KSKAKatsudouResult alloc]init];
    result = [_dataArray objectAtIndex:indexPath.item];
    cell.katsudou = result;
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    //KSKLog(@"%f",(_collectionView.height - topViewHeight - segmentHeight - (_dataArray.count - 1) * chartMarginHeight - xAxis) / _dataArray.count);
    return CGSizeMake(_collectionView.width,_collectionView.height / 8);
}

@end
